﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using System.Data.SqlClient;
using MySQLDriverCS;
using MySql.Data.MySqlClient;
using System.Data.Odbc;
using System.Data.SQLite;
using System.Data.OleDb;

namespace sqlconnectionstudy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnoracle_Click(object sender, EventArgs e)
        {
            bool IsCanConnectioned = false;
            string ConnectionString = "Data Source=orcl;Persist Security Info=True;User ID=viot;Password=viot ";
            OracleConnection conn = new OracleConnection(ConnectionString);
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnoracle1_Click(object sender, EventArgs e)
        {
            bool IsCanConnectioned = false;
            string ConnectionString = "Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.12.204)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl)));Persist Security Info=True;User ID=viot;Password=viot";
            OracleConnection conn = new OracleConnection(ConnectionString);
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnsqlserver_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "user id=viot;password=viot;DataBase=viot;server=192.168.12.204;connect Timeout=30";
            bool IsCanConnectioned = false;
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnsqlserver1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Integrated Security=true;DataBase=viot;server=192.168.12.204;connect Timeout=30";
            bool IsCanConnectioned = false;
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnmysql_Click(object sender, EventArgs e)
        {
            //string ConnectionString = "Database=VIOT;Data Source=192.168.12.204;User Id=viot;Password=viot;pooling=false;CharSet=utf8;port=3306";
            MySQLConnection conn = new MySQLConnection(new MySQLConnectionString ("192.168.12.204", "viot", "viot", "viot", 3306).AsString);//ConnectionString);
            bool IsCanConnectioned = false;
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnmysql1_Click(object sender, EventArgs e)
        {
            string ConnectionString = "Data Source=192.168.12.204;Database=viot;User ID=viot;Password=viot;pooling=false;CharSet=utf8;port=3306";
            MySqlConnection conn = new MySqlConnection(ConnectionString);
            //MySQLConnection conn = new MySQLConnection(ConnectionString);
            bool IsCanConnectioned = false;
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnmysql2_Click(object sender, EventArgs e)
        {
            string ConnectionString = "driver={MySQL ODBC 5.1 Driver};"+"server=192.168.12.204;"+"database=viot;"+"uid=viot;"+"password=viot;"+"Option=3";
            OdbcConnection conn = new OdbcConnection(ConnectionString);
            bool IsCanConnectioned = false;
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnsqlite_Click(object sender, EventArgs e)
        {
            string ConnectionString = @"Data Source=E:\sqlite\VIOT.db;Pooling=true;FailIfMissing=false";
            SQLiteConnection conn = new SQLiteConnection(ConnectionString);
            bool IsCanConnectioned = false;
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnaccess_Click(object sender, EventArgs e)
        {
            string ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;"+ @"Data Source=E:\accessdb\VIOT.mdb";
            OleDbConnection conn = new OleDbConnection(ConnectionString);
            bool IsCanConnectioned = false;
            try
            {
                conn.Open();
                IsCanConnectioned = true;
            }
            catch
            {
                IsCanConnectioned = false;
            }
            finally
            {
                conn.Close();
            }

            if (IsCanConnectioned)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show("连接不成功");
            }
        }

        private void btnuseoracle_Click(object sender, EventArgs e)
        {
            oraclechange frm = new oraclechange();
            frm.ShowDialog();
        }
    }
}
